local qunyu = fk.CreateSkill {
  name = "cf_qunyu",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable {
  ["cf_qunyu"] = "群玉",
  [":cf_qunyu"] = "限定技，出牌阶段，你可以于接下来五轮获得【群玉阁】。若你拥有群玉阁，你可于出牌阶段将任意张牌置于武将牌上（每个回合限一次），称为【阁】你可以将武将牌上的【阁】如手牌般的使用或打出。其他角色的出牌阶段开始时，你可以失去【群玉阁】和所有【阁】，然后对其造成【阁】中牌数的伤害，若该角色因此死亡，此技能视为未发动。",
  ["@cf_qunyu_count"] = "群玉阁",
  ["#cf_qunyu_pile"] = "将任意张牌置于武将牌上称为阁",
  ["cf_qunyu_card"] = "阁",
}

qunyu:addEffect("active", {
  mute = true,
  card_num = 0,
  target_num = 0,
  anim_type = "offensive",
  can_use = function(self, player)
    if player:usedSkillTimes(qunyu.name, Player.HistoryGame) == 0 then
      return player:hasSkill(qunyu.name)
    else
      return player:hasSkill(qunyu.name) and player:usedSkillTimes(qunyu.name, Player.HistoryTurn) == 0 and
          player:getMark("@cf_qunyu_count") > 0
    end
  end,
  on_cost = function(self, player, data, extra_data)
    if player:usedSkillTimes(qunyu.name, Player.HistoryGame) == 0 then return end
    local room = player.room
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = 999,
      skill_name = qunyu.name,
      prompt = "#cf_qunyu_pile",
      cancelable = false,
    })
    return { cards = cards, }
  end,
  on_use = function(self, room, skillUseEvent)
    local player = skillUseEvent.from
    if player:getMark("@cf_qunyu_count") == 0 then
      room:setPlayerMark(player, "@cf_qunyu_count", 5)
      player:setSkillUseHistory(qunyu.name, 0, Player.HistoryTurn)
      room:notifySkillInvoked(player, qunyu.name, "big")
      player:setSkillUseHistory(qunyu.name, 2, Player.HistoryGame)
      return
    end
    local cards = skillUseEvent.cost_data.cards ---@type integer[]
    player:addToPile("cf_qunyu_card", cards, false, qunyu.name)
  end,
})

qunyu:addEffect(fk.RoundEnd, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(qunyu.name) and player:getMark("@cf_qunyu_count") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@cf_qunyu_count", 1)
  end
})

qunyu:addEffect("filter", {
  handly_cards = function(self, player)
    if player:hasSkill(qunyu.name) then
      return player:getPile("cf_qunyu_card")
    end
  end,
})

qunyu:addEffect(fk.EventPhaseStart, {
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@cf_qunyu_count") > 0 and player:hasSkill(qunyu.name) and player ~= target and
        target.phase == Player.Play
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@cf_qunyu_count", 0)
    local choice = room:askToChoice(player, {
      choices = { "normal_damage", "thunder_damage", "fire_damage", "ice_damage" },
      skill_name = qunyu.name,
      cancelable = false,
    })
    local damageType = {
      normal_damage = fk.NormalDamage,
      thunder_damage = fk.ThunderDamage,
      fire_damage = fk.FireDamage,
      ice_damage = fk.IceDamage,
    }
    local damage = #player:getPile("cf_qunyu_card")
    player:clearPiles()
    room:damage({
      from = player,
      to = target,
      damage = damage,
      damageType = damageType[choice]
    })
    if target.dead then
      player:setSkillUseHistory(qunyu.name, 0, Player.HistoryGame)
    end
  end
})

return qunyu
